import { Http, http } from "@/utils/http";

const configDefault = {
  headers: {
    "Content-Type": "application/json",
  },
  timeout: 30000,
  baseURL: import.meta.env.VITE_XMATE_BASE_API,
  data: {},
};
// 这里域名变了使用新的
export const xmateHttp = new Http(configDefault);

// 搜索公司名称 企查查
export const searchCompanyAPI = name => {
  const data = {
    companyName: name,
  };
  return http.request({
    url: "rest/api/company/search",
    method: "POST",
    data,
  });
};

export const searchInnerCompanyAPI = name => {
  const data = {
    companyName: name,
  };
  return http.request({
    url: "rest/api/customer/search",
    method: "POST",
    data,
  });
};

// 高德地图搜索地点
export const searchAMapAPI = async name => {
  // 这里使用Axios 会跨域（因为配置了baseurl） ，所以使用fetch
  const response = await fetch(
    `https://restapi.amap.com/v5/place/text?key=ae2aa31ee60878848c59890268ff8443&keywords=${name}`
  );
  // 检查响应状态
  if (!response.ok) {
    throw new Error("网络响应错误: " + response.status);
  }
  const data = await response.json(); // 解析 JSON 数据
  return data;
};

// 模糊查找人员 includeChildren: 1 表示包含子部门 0 表示不包含子部门
// export const searchInnerAttendAPI = (name, deptIds = []) => {
//   const data = {
//     name: name,
//     deptIds,
//   };
//   return xmateHttp.request({
//     url: "rest/api/conference/user/search",
//     method: "POST",
//     data,
//   });
// };

// 模糊查询内部参会人 [组织架构内][组织架构外]
export const searchInnerAttendAPI = (name) => {
  return http.request({
    url: "/rest/api/v1/partners/search",
    method: "GET",
    params: { name, size: 999 },
  });
};

export const searchInnerAttendPagerAPI = param => {
  let {
    name = "",
    deptIds = [],
    includeChildren = 0,
    queryManagedDepartment = false,
    pageSize = 99999,
    pageNumber = 1,
  } = param;
  const data = {
    pageNumber,
    pageSize,
    deptIds,
    orderBy: "",
    searchKey: "",
    name,
    asc: true,
    queryManagedDepartment,
    includeChildren: includeChildren,
  };
  return xmateHttp.request({
    url: "rest/api/conference/partner/search/pager",
    method: "POST",
    data,
    timeout: 90000,
  });
};

// 查找公司人员列表
export const searchCustomerAttendAPI = id => {
  const data = {
    customerId: id,
  };
  return xmateHttp.request({
    url: "rest/api/xmate/customer/contact",
    method: "POST",
    data,
  });
};

// 搜索商品列表
//https://dev.x-mate.com/rest/api/conference/goods/category/1839139876046376960/list
export const productListAPI = (
  categoryId,
  data = {
    searchKey: "",
    pageSize: 20,
    pageNumber: 1,
  }
) => {
  return xmateHttp.request({
    url: `/rest/api/conference/goods/category/${categoryId}/list`,
    method: "POST",
    data,
  });
};

// 查询预约沟通配置
export const getConfigAPI = () => {
  return xmateHttp.request({
    url: "rest/api/xmate/position/config",
    method: "GET",
  });
};

// 新建客户联系人
export const createCustomerAttendAPI = (data, customerId) => {
  return xmateHttp.request({
    url: `rest/api/xmate/customer/${customerId}/contact`,
    method: "POST",
    data,
  });
};

// 上传文件
export const uploadFileAPI = data => {
  return xmateHttp.request({
    url: "rest/api/conference/schedule/file",
    method: "POST",
    data,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

// 新建沟通记录
export const createVisitRecordAPI = data => {
  return xmateHttp.request({
    url: "rest/api/xmate/user/plans/schedule",
    method: "POST",
    data,
  });
};

// 修改沟通记录
export const updateVisitAPI = data => {
  return xmateHttp.request({
    url: `rest/api/xmate/user/plans/${data.scheduleId}`,
    method: "PUT",
    data,
  });
};

// 沟通记录
export const visitListAPI = data => {
  return xmateHttp.request({
    url: "rest/api/xmate/user/plans",
    method: "POST",
    data,
  });
};

// 沟通详情
export const visitDetailsAPI = scheduleId => {
  return xmateHttp.request({
    url: `/rest/api/xmate/user/plans/${scheduleId}`,
    method: "GET",
  });
};

// 沟通详情 访前准备
export const preparVisitAPI = scheduleId => {
  return xmateHttp.request({
    url: `/rest/api/xmate/user/plans/${scheduleId}/prepare`,
    method: "GET",
  });
};

// 获取客户id
export const getCompanyId = (companyName = "") => {
  if (companyName.length === 0) {
    console.warn("getCompanyId companyName is null.");
    return;
  }
  const data = { companyName };
  return http.request({
    url: "/rest/api/company/",
    method: "POST",
    data,
  });
};

// 获取所有沟通环节
// 类型：0 客户 1 沟通流程
export const getCommunicationStagesAPI = ({ type = 0, targetId = "" }) => {
  return xmateHttp.request({
    url: "/rest/api/v1/communication/flows/stages",
    method: "GET",
    params: {
      type,
      targetId,
    },
  });
};

// 获取商品详情
export const getProductDetailAPI = (productIds = []) => {
  return xmateHttp.request({
    url: "/rest/api/conference/goods/details",
    method: "POST",
    data: { ids: productIds },
  });
};

// 查询推荐人
export const searchRecommendAPI = () => {
  return xmateHttp.request({
    url: "/rest/api/participant/recommend",
    method: "GET",
  });
};




// 获取外部人员 动态字段
export const getOuterFiledPartnerAPI = () => {
  return xmateHttp.request({
    url: "/rest/api/form/definition/fields?formCode=OUTER_PARTNER",
    method: "GET",
  });
};

// 获取内部人员 动态字段
export const getInternalFiledPartnerAPI = () => {
  return xmateHttp.request({
    url: "/rest/api/form/definition/fields?formCode=INTERNAL_PARTNER",
    method: "GET",
  });
};

// 搜索客户类型列表
export const searchCustomerType = (status = 0) => {
  return xmateHttp.request({
    url: "/rest/api/v1/customer-type/list",
    method: "GET",
  });
};

// 获取新建客户类型 动态字段
export const getCustomerTypeAPI = () => {
  return xmateHttp.request({
    url: `/rest/api/form/definition/fields?formCode=CUSTOMER_TPL`,
    method: "GET",
  });
};

// 创建客户
export const createCustomerAPI = data => {
  return xmateHttp.request({
    url: "/rest/api/customer",
    method: "POST",
    timeout: 6000000,
    data,
  });
};

// 编辑客户
export const updateCustomerAPI = (customerId, data) => {
  return xmateHttp.request({
    url: `/rest/api/customer/${customerId}`,
    method: "PUT",
    data,
  });
};

// 客户列表
export const searchCustomerListAPI = (searchText = "") => {
  return xmateHttp.request({
    url: "/rest/api/customer/search",
    method: "POST",
    data: {
      companyName: searchText,
    },
  });
};


/// 新建联系人 【内部 非组织架构内】
export function createInternalAttendAPI(parameter) {
  return http.request({
    url: `/rest/api/v1/partners`,
    method: "POST",
    data: parameter,
  });
}
